package test.file.utils;

import org.apache.http.util.TextUtils;

import java.io.*;
import java.nio.channels.FileChannel;

public class MergeFile {

	/**
	 * 利用BufferedStream合并多个文件
	 * @param fpaths
	 * @param resultPath
	 * @return
	 */
	public static boolean mergeFilesForIo(String[] fpaths, String resultPath, boolean deleteFile) {
	    if (fpaths == null || fpaths.length < 1 || TextUtils.isEmpty(resultPath)) {
	        return false;
	    }
	    if (fpaths.length == 1) {
	        return new File(fpaths[0]).renameTo(new File(resultPath));
	    }
	 
	    File[] files = new File[fpaths.length];
	    for (int i = 0; i < fpaths.length; i ++) {
//			if(StringUtils.isEmpty(fpaths[i]) || fpaths[i].equals("null")){
//				continue;
//			}
	        files[i] = new File(fpaths[i]);
	        if (TextUtils.isEmpty(fpaths[i]) || !files[i].exists() || !files[i].isFile()) {
	            return false;
	        }
	    }
	 
	    File resultFile = new File(resultPath);
	 
	    try {
	        int bufSize = 1024;
	        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(resultFile));
	        byte[] buffer = new byte[bufSize];
	 
	        for (int i = 0; i < fpaths.length; i ++) {
//				if(StringUtils.isEmpty(fpaths[i]) || fpaths[i].equals("null")){
//					continue;
//				}
	            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(files[i]));
	            int readcount;
	            while ((readcount = inputStream.read(buffer)) > 0) {
	                outputStream.write(buffer, 0, readcount);
	            }
	            inputStream.close();
	        }
	        outputStream.close();
	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	        return false;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return false;
	    }
	 
	    if(deleteFile) {
		    for (int i = 0; i < fpaths.length; i ++) {
		        files[i].delete();
		    }
	    }
	 
	    return true;
	}
	

	/**
	 * 利用nio FileChannel合并多个文件
	 * @param fpaths
	 * @param resultPath
	 * @return
	 */
	public static boolean mergeFilesForNio(String[] fpaths, String resultPath) {
	    if (fpaths == null || fpaths.length < 1 || TextUtils.isEmpty(resultPath)) {
	        return false;
	    }
	    if (fpaths.length == 1) {
	        return new File(fpaths[0]).renameTo(new File(resultPath));
	    }
	 
	    File[] files = new File[fpaths.length];
	    for (int i = 0; i < fpaths.length; i ++) {
	        files[i] = new File(fpaths[i]);
	        if (TextUtils.isEmpty(fpaths[i]) || !files[i].exists() || !files[i].isFile()) {
	            return false;
	        }
	    }
	 
	    File resultFile = new File(resultPath);
	 
	    try {
	        FileChannel resultFileChannel = new FileOutputStream(resultFile, true).getChannel();
	        for (int i = 0; i < fpaths.length; i ++) {
	            FileChannel blk = new FileInputStream(files[i]).getChannel();
	            resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size());
	            blk.close();
	        }
	        resultFileChannel.close();
	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	        return false;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return false;
	    }
	 
	    for (int i = 0; i < fpaths.length; i ++) {
	        files[i].delete();
	    }
	 
	    return true;
	}
	
}
